package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.PublicKey;
import java.util.ArrayList;

/**
 * Created by kiit on 7/3/14.
 */
public class Library {
    private ArrayList<Book> BookList= new ArrayList<Book>();
    private ArrayList<Movie> MovieList= new ArrayList<Movie>();
    private ArrayList<User> UserList= new ArrayList<User>();

    public ArrayList<User> getUserList() {
        return UserList;
    }

    public void addUserToList(User U){
        UserList.add(U);
    }

    public void ViewAllUsers(){
        System.out.printf("%12s \t %30s \t %30s ","Library Card No.","Name.","Contact.");
        System.out.println("=========\t ===========\t ==========\t ========\t ===========");
        for(User U:UserList){
            System.out.printf(" %12s \t %30s \t %30s\n",U.getLibraryCardNo(),U.getName(),U.getContact());
        }
    }

    public User getUser(String LibraryCardNo){
        for(User U:UserList) {
            if (U.getLibraryCardNo().equals(LibraryCardNo)) {
                return U;
            }
        }
        return null;
    }

    public User UserLogin() throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufRead = new BufferedReader(inputStreamReader);
        System.out.println("\nEnter Library Card No. ");
        String LibraryCardNo= bufRead.readLine();
        User U= getUser(LibraryCardNo);
        System.out.println("\nEnter Password.  ");
        String password= bufRead.readLine();
        if(U.VerifyPassword(password)){
            return U;
        }
        return null;
    }

    public Library(){
        Book b1=new Book(1,"Java For You","A Complete Java Tutorial","Micheal","");
        addBookToList(b1);
        b1=new Book(2,".Net in 21 days","Fast paced trainer","Micheal","");
        addBookToList(b1);
        b1=new Book(3,"2 States","A Tale of 2 lovers","Chetan Bhagat","");
        addBookToList(b1);
        b1=new Book(4,"Ruby On Rails Tutorial","Learn Web Development with Rails","Micheal Hartl","");
        addBookToList(b1);
        b1=new Book(5,"Introduction to algorithms","All about algorithms","T Cormen","");
        addBookToList(b1);

        Movie m1=new Movie(1,"Inception","Sci-Fi","Cristopher Nolan",9,"");
        addMovieToList(m1);
        m1=new Movie(2,"Dark Knight Rises","Superhero","Cristopher Nolan",9,"");
        addMovieToList(m1);
        m1=new Movie(3,"2 States","Drama- A Tale of 2 lovers","Abhishek Verman",7,"");
        addMovieToList(m1);
        m1=new Movie(4,"My Name Is Khan","Drama ","Karan Johar",8,"");
        addMovieToList(m1);
        m1=new Movie(5,"Jhonny English","Action/Comedy","Peter Howitt",6,"");
        addMovieToList(m1);

        User u1= new User("111-0001","abcde","Ravi","9811111111");
        addUserToList(u1);
        u1= new User("111-0002","abcde","Teja","9821111111");
        addUserToList(u1);
    }
    public ArrayList<Book> getBookList() {
        return BookList;
    }

    public void addBookToList(Book B){
        BookList.add(B);
    }

    public Book getBook(int BookNo){
        for(Book B:BookList) {
            if (B.getBookNo() == BookNo) {
                return B;
            }
        }
        return null;
    }

    public void ViewAvailableBookList(){
        System.out.printf("%8s \t %30s \t %30s \t Description \n","Book No.","Book Name.","Author.");
        System.out.println("=========\t ===========\t ========\t ===========");
        for(Book B:BookList){
            if(B.getReservedTo().equals(""))
            System.out.printf(" %8d \t %30s \t %30s \t %s \n",B.getBookNo(),B.getBookName(),B.getAuthor(),B.getDescription());
        }
    }

    public void ViewAllBooks(){
        System.out.printf("%8s \t %30s \t %30s \t Reserved To. \t Description \n","Book No.","Book Name.","Author.");
        System.out.println("=========\t ===========\t ==========\t ========\t ===========");
        for(Book B:BookList){
                System.out.printf(" %8d \t %30s \t %30s \t %10s \t %s \n",B.getBookNo(),B.getBookName(),B.getAuthor(),B.getReservedTo(),B.getDescription());
        }
    }

    public void CheckOutBook(int BookNo, User U) throws IOException {
        Book B=getBook(BookNo);

        if(U==null) {
            System.out.println("Invalid Login Try Again");
            return;
        }
        if(B!=null) {
            if (B.getReservedTo().equals("")) {
                B.setReservedTo(U.getLibraryCardNo());
                System.out.printf("The Book %s with Book No %d is Checked Out to %s \n", B.getBookName(), B.getBookNo(),U.getLibraryCardNo());
            } else {
                System.out.println("The Book is already reserved");
            }
        }
        else
            System.out.println("Invalid Book No");
    }

    public void ReturnBook(int BookNo, User U) throws IOException {
        Book B=getBook(BookNo);

        if(U==null) {
            System.out.println("Invalid Login Try Again");
            return;
        }
        if(B!=null) {
            if (B.getReservedTo().equals(U.getLibraryCardNo())) {
                B.setReservedTo("");
                System.out.printf("The Book %s with Book No %d is Returned from %s \n", B.getBookName(), B.getBookNo(),U.getLibraryCardNo());
            } else {
                System.out.println("The Book is either not reserved or not reserved to you");
            }
        }
        else
            System.out.println("Invalid Book No");
    }

    public ArrayList<Movie> getMovieList() {
        return MovieList;
    }

    public void addMovieToList(Movie M){
        MovieList.add(M);
    }

    public Movie getMovie(int MovieId){
        for(Movie M:MovieList) {
            if (M.getMovieId() == MovieId) {
                return M;
            }
        }
        return null;
    }

    public void ViewAvailableMovieList(){
        System.out.printf("%8s \t %30s \t %30s \t Description \n","Movie Id.","Movie Name.","Director.");
        System.out.println("=========\t ===========\t ========\t ===========");
        for(Movie M:MovieList){
            if(M.getReservedTo().equals(""))
                System.out.printf(" %8d \t %30s \t %30s \t %s \n",M.getMovieId(),M.getMovieName(),M.getDirector(),M.getDescription());
        }
    }

    public void ViewAllMovies(){
        System.out.printf("%8s \t %30s \t %30s \t Reserved To. \t Description \n","Movie No.","Movie Name.","Director.");
        System.out.println("=========\t ===========\t ==========\t ========\t ===========");
        for(Movie M:MovieList){
            System.out.printf(" %8d \t %30s \t %30s \t %10s \t %s \n",M.getMovieId(),M.getMovieName(),M.getDirector(),M.getReservedTo(),M.getDescription());
        }
    }

    public void CheckOutMovie(int MovieId, User U) throws IOException {
        Movie M=getMovie(MovieId);

        if(U==null) {
            System.out.println("Invalid Login Try Again");
            return;
        }
        if(M!=null) {
            if (M.getReservedTo().equals("")) {
                M.setReservedTo(U.getLibraryCardNo());
                System.out.printf("The Movie %s with Movie No %d is Checked Out to %s\n", M.getMovieName(), M.getMovieId(), U.getLibraryCardNo());
            } else {
                System.out.println("The Movie is already reserved");
            }
        }
        else
            System.out.println("Invalid Movie No");
    }

    public void ReturnMovie(int MovieNo, User U) throws IOException {
        Movie M=getMovie(MovieNo);

        if(U==null) {
            System.out.println("Invalid Login Try Again");
            return;
        }
        if(M!=null) {
            if (M.getReservedTo().equals(U.getLibraryCardNo())) {
                M.setReservedTo("");
                System.out.printf("The Movie %s with Movie No %d is Returned by %s\n", M.getMovieName(), M.getMovieId(),U.getLibraryCardNo());
            } else {
                System.out.println("The Movie is not reserved");
            }
        }
        else
            System.out.println("Invalid Movie No");
    }
}
